{% extends 'layout/basic.html' %}
{% load static %}

{% block title %}
<title>注册</title>
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}


{% block content %}
    <div class="account">
        <div class="title">用户注册</div>
        <form id="regForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="注  册"/>
                </div>
            </div>
        </form>
    </div>
{% endblock %}


{% block js %}
    <script>
        // 页面框架加载完成之后自动执行函数
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        })

        // 点击获取验证码，发送短信
        function bindClickBtnSms() {
            $("#btnSms").click(function () {
                // 清空错误提示信息
                $(".error-msg").empty();

                var mobile_phone = $("#id_mobile_phone").val();

                $.ajax({
                    url: "{% url 'send_sms' %}",
                    type: 'GET',
                    data: {mobile_phone: mobile_phone, tpl: "register"},
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            sendSmsRemind();
                        } else {
                            $.each(res.err, function (key, value) {
                                $("#id_" + key).next().text(value[0])
                            });
                        }
                    }
                })
            });
        }

        // 发送短信
        function sendSmsRemind() {
            var $btnSms = $("#btnSms");
            // 不能再次点击发送验证码按钮
            $btnSms.prop("disabled", true);
            var time = 60
            var timer = setInterval(function () {
                time = time - 1;
                $btnSms.val(time)
                if (time < 1) {
                    // 清空定时器，重新发送短信验证码
                    clearInterval(timer)
                    $btnSms.prop("disabled", false);
                    $btnSms.val("点击获取验证码")
                }
            }, 1000)
        }

        // 提交form表单数据
        function bindClickSubmit() {
            $("#btnSubmit").click(function () {
                // 收集表单中的数据
                // 数据ajax发送到后台
                $(".error-msg").empty();

                $.ajax({
                    url: "{% url 'register' %}",
                    type: 'POST',
                    data: $("#regForm").serialize(),  // 所有字段数据 + csrf_token
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.err, function (key, value) {
                                $("#id_" + key).next().text(value[0])
                            });
                        }
                    }
                });
            });
        }
    </script>

{% endblock %}